//
//  SKAFNetWork.swift
//  ScarnKing
//
//  Created by ninebot on 2025/7/22.
//

import Foundation
import Alamofire

class SKAFNetWork: NSObject {
    // MARK: - 原有百度API方法保持不变
    
    func getBuduAccessToken () {
        let parameters: [String: Any] = [
            "client_id": kSKBudu_Appkey,
            "client_secret" : kSKBudu_AppSecret,
            "grant_type" : "client_credentials"
        ]
        let headers: HTTPHeaders = [
            "Content-Type": "application/json",
            "Accept": "application/json"
        ]
        AF.request("https://aip.baidubce.com/oauth/2.0/token", method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers)
            .validate()
            .responseJSON { response in
                switch response.result {
                case .success(let value):
                    if value != nil{
                       let data = value as! Dictionary<String, Any>
                        if let accessToken = data["access_token"] as? String {
                            print("百度-token-\(accessToken)")
//                            self.buduToken = accessToken
                            UserDefaults.standard.setValue(accessToken, forKey: "BuDuAccessToken");
                        } else {
                            print("Access Token not found in response")
                        }

//                        successHandler(value as! Dictionary<String, Any>)
                        return
                    }else{

//                        successHandler(json)
                        return
                    }
                case .failure(let error):
                    print(error)
                }
            }
    }
    //文档识别
    func photoToText(imageBase64: String,successHandler: @escaping(_ json:Dictionary<String, Any>) ->(),errorMsgHandler : @escaping(_ errorMsg : String) ->(),networkFailHandler:@escaping(_ error : Error) -> ()) {
      
        guard let accessToken = UserDefaults.standard.object(forKey: "BuDuAccessToken") as? String else{return}
         
        // 设置请求头
        let headers: HTTPHeaders = [
            "Content-Type": "application/x-www-form-urlencoded"
        ]
        
        // 构建请求参数
        let parameters: [String: Any] = [
            "image": imageBase64,
            "access_token" : accessToken,
        ]
        
        
        // 发送网络请求
        AF.request(kSKBudu_accurate_basic_url, method: .post, parameters: parameters, encoding: URLEncoding.httpBody, headers: headers)
            .validate()
            .responseJSON { response in
                switch response.result {
                case .success(let value):
                    if value != nil{
                        successHandler(value as! Dictionary<String, Any>)
                        return
                    }else{
                        return
                    }
                case .failure(let error):
                    print(error)
                }
            }
    }

    //文档识别
    func texttransToLanage(text: String, to : String,successHandler: @escaping(_ json:Dictionary<String, Any>) ->(),errorMsgHandler : @escaping(_ errorMsg : String) ->(),networkFailHandler:@escaping(_ error : Error) -> ()) {
      
        guard let accessToken = UserDefaults.standard.object(forKey: "BuDuAccessToken") as? String else{return}
         
        // 设置请求头
        let headers: HTTPHeaders = [
            "Content-Type": "application/json;charset=utf-8",
            "Accept":"application/json"
        ]
        
        // 构建请求参数
        let parameters: [String: Any] = [
            "q": text,
            "access_token" : accessToken,
            "from" : "auto",
            "to" : to,
        ]
        
        
        // 发送网络请求
        AF.request(kSKBudu_trans_basic_url, method: .post, parameters: parameters, encoding: URLEncoding.queryString, headers: headers)
            .validate()
            .responseJSON { response in
                switch response.result {
                case .success(let value):
                    if value != nil{
                        successHandler(value as! Dictionary<String, Any>)
                        return
                    }else{
                        return
                    }
                case .failure(let error):
                    print(error)
                }
            }
    }

}
